package zcxn.microService.service;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.codec.binary.Base64;
import org.apache.ibatis.session.SqlSession;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import zcxn.microService.bean.BattParamsInfo;
import zcxn.microService.bean.CommunicatInfo;
import zcxn.microService.bean.GlobalAllConfigInfo;
import zcxn.microService.bean.GlobalPartConfigInfo;
import zcxn.microService.bean.LoadInfo;
import zcxn.microService.bean.PVInfo;
import zcxn.microService.bean.SPHParamsInfo;
import zcxn.microService.dao.BattParametersMapper;
import zcxn.microService.dao.GlobalConfigMapper;
import zcxn.microService.dao.SPHParametersMapper;
import zcxn.microService.daoUtil.DaoUtil;

public class GetConfigParaBean {
	static final String globalPro = "/global.properties";
	//通过agentId获取所有的全局配置参数
		public static GlobalAllConfigInfo getGlobalAllConfigInfo(String agentId) throws IOException {
			GlobalPartConfigInfo partGlobal = getPartGlobal(agentId);
			YamlPropertiesUtil yamlPropertiesUtil=new YamlPropertiesUtil();
			Map<String, Object> map = yamlPropertiesUtil.loadProPerties(globalPro);
			System.out.println(map);
			GlobalAllConfigInfo allConfigInfo=new GlobalAllConfigInfo();
			allConfigInfo.setCollFreq(Integer.parseInt(((String) map.get("coll_freq")).trim()) );
			allConfigInfo.setSocUpdateCycle(Integer.parseInt(((String) map.get("soc_update_cycle")).trim()));
			allConfigInfo.setCacheCycle(Integer.parseInt(((String)map.get("cache_cycle")).trim()));
			allConfigInfo.setUploadRetryTimes(Integer.parseInt(((String)map.get("upload_retry_times")).trim()));
			allConfigInfo.setFloatingVoltDev(Integer.parseInt(((String)map.get("floating_volt_dev")).trim()));
			allConfigInfo.setFloatingHoldTime(Integer.parseInt(((String)map.get("floating_hold_time")).trim()));
			allConfigInfo.setServiceName(partGlobal.getServiceName());
			allConfigInfo.setGridId(partGlobal.getGridId());
			allConfigInfo.setAgentId(partGlobal.getAgentId());
			allConfigInfo.setDataCheckType(partGlobal.getCommunicatinfo().getDataCheckType());
			allConfigInfo.setCheckBit(partGlobal.getCommunicatinfo().getCheckBit());
			allConfigInfo.setDataBit(partGlobal.getCommunicatinfo().getDataBit());
			allConfigInfo.setStopBit(partGlobal.getCommunicatinfo().getStopBit());
			allConfigInfo.setTransMode(partGlobal.getCommunicatinfo().getTransMode());
			allConfigInfo.setBaudRate(partGlobal.getCommunicatinfo().getBaudRate());
			allConfigInfo.setSlaveAddress(partGlobal.getCommunicatinfo().getSlaveAddress());
			allConfigInfo.setEleAPort(partGlobal.getCommunicatinfo().getEleAPort());
			allConfigInfo.setEleInstanceAId(partGlobal.getEleInstanceAId());
			allConfigInfo.setEleInstanceBId(partGlobal.getEleInstanceBId());
			allConfigInfo.setRelDirect(partGlobal.getRelDirect());
			allConfigInfo.setBattEleInstanceId(partGlobal.getBattEleInstanceId());
			allConfigInfo.setPv1EleInstanceId(partGlobal.getPv1EleInstanceId());
			allConfigInfo.setPv2EleInstanceId(partGlobal.getPv2EleInstanceId());
			allConfigInfo.setBackupEleInstanceId(partGlobal.getBackupEleInstanceId());
			allConfigInfo.setNormalEleInstanceId(partGlobal.getNormalEleInstanceId());
			allConfigInfo.setPiccEleInstanceId(partGlobal.getPiccEleInstanceId());
			allConfigInfo.setEdgeEleInstanceId(partGlobal.getEdgeEleInstanceId());
			allConfigInfo.setSphEleInstanceId(partGlobal.getSphEleInstanceId());
			allConfigInfo.setSphPassword(partGlobal.getSphPassword());
			allConfigInfo.setNewBatt(Boolean.parseBoolean(((String) map.get("NEW_BATT")).trim()));
			allConfigInfo.setSmartMaterAddress(partGlobal.getSmartMaterAddress());
			allConfigInfo.setExtennalSensor(partGlobal.getExtennalSensor());
			allConfigInfo.setMaxUploadNum(Integer.parseInt(((String)map.get("MAX_UPLOAD_NUM")).trim()));
			allConfigInfo.setDataLoggerSPHRS485(Integer.parseInt(((String)map.get("DATALOGGER_SPH_RS485")).trim()));
			allConfigInfo.setDataLoggerSPHRS485(Integer.parseInt(((String)map.get("DATA_TOUPLOAD")).trim()));
			allConfigInfo.setReportUrl(((String)map.get("REPORT_URL")).trim());
			allConfigInfo.setFloatTimes(Integer.parseInt(((String)map.get("FLOAT_TIMES")).trim()));
			allConfigInfo.setClearUploadDataAgo(Integer.parseInt(((String)map.get("CLEAR_UPLOAD_DATA_AGO")).trim()));
			return allConfigInfo;
		}
//		SPH参数
		public static SPHParamsInfo getSphParamsInfo(String sphId) {
			SqlSession sqlSession = DaoUtil.getSqlSession();
			SPHParametersMapper mapper = sqlSession.getMapper(SPHParametersMapper.class);
			return mapper.selectSPHParam(sphId);
			
		}
	//储能参数
		public static BattParamsInfo getBattParamsInfo(String battId) {
			SqlSession sqlSession = DaoUtil.getSqlSession();
			BattParametersMapper mapper = sqlSession.getMapper(BattParametersMapper.class);
			return mapper.selectBattParam(battId);
		}
		//部分全局配置参数
		public static GlobalPartConfigInfo getPartGlobal(String agentId) throws IOException {
			SqlSession sqlSession = DaoUtil.getSqlSession();
			GlobalConfigMapper mapper = sqlSession.getMapper(GlobalConfigMapper.class);
			GlobalPartConfigInfo partGlobal = mapper.selectGridId(agentId);
			if (partGlobal == null) {
				System.out.println("no grid  for agentId");
				System.exit(3);
			}

			List<PVInfo> selectPvId = mapper.selectPvId(partGlobal.getGridId());
			if (selectPvId.isEmpty()) {
				System.out.println("no Pv for gridId");
				System.exit(2);
			}
			for (int i = 0; i < selectPvId.size(); i++) {
				int b = selectPvId.get(i).getPvOrder();
				if (b == 1) {
					partGlobal.setPv1EleInstanceId(selectPvId.get(i).getPvId());
				} else {
					partGlobal.setPv2EleInstanceId(selectPvId.get(i).getPvId());
				}
			}
			List<LoadInfo> selectLoadId = mapper.selectLoadId(partGlobal.getGridId());
			if (selectLoadId.isEmpty()) {
				System.out.println("no load for gridId");
				System.exit(2);
			}
			for (int i = 0; i < selectLoadId.size(); i++) {
				int loadClass = selectLoadId.get(i).getLoadClass();
				if (loadClass == 1000) {
					partGlobal.setNormalEleInstanceId(selectLoadId.get(i).getLoadId());
				} else {
					partGlobal.setBackupEleInstanceId(selectLoadId.get(i).getLoadId());
				}
			}
			partGlobal.setBattEleInstanceId(mapper.selectBattId(partGlobal.getGridId()));
			partGlobal.setPiccEleInstanceId(mapper.selectPiccId(partGlobal.getGridId()));
			partGlobal.setEdgeEleInstanceId(partGlobal.getEleInstanceAId());
			partGlobal.setSphEleInstanceId(partGlobal.getEleInstanceBId());
			partGlobal.setAgentId(agentId);
			partGlobal.setSmartMaterAddress(1);
			partGlobal.setExtennalSensor(2);
			String sy = mapper.selectSPHPassword(partGlobal.getSphEleInstanceId());
			byte[] pass = new byte[4];
			Integer in = Integer.parseInt(sy);
			byte[] intToByte4 = intToByte4(in);
			String pwd = Base64.encodeBase64String(intToByte4);
			partGlobal.setSphPassword(pwd);
			CommunicatInfo Comm = mapper.selectComm(partGlobal.getEdgeEleInstanceId());
			if (Comm == null) {
				System.out.println("no CommunicatInfo for DataLogger");
				System.exit(2);
			}
			partGlobal.setCommunicatinfo(Comm);
			partGlobal.setServiceName(agentId.concat(partGlobal.getCommunicatinfo().getEleAPort().replaceAll("/", "_")));
			System.out.println(partGlobal.toString());
			return partGlobal;
		}

		// int 求base64 SPHPassword
		public static byte[] intToByte4(int i) {
			byte[] targets = new byte[4];
			targets[3] = (byte) (i & 0xFF);
			targets[2] = (byte) (i >> 8 & 0xFF);
			targets[1] = (byte) (i >> 16 & 0xFF);
			targets[0] = (byte) (i >> 24 & 0xFF);
			return targets;
		}
}
